'use strict';
app.controller('UserInfoCtrl', function($scope,$state,LocalStorageService,USERAUTHDATA,UserService,TokenService,$ionicActionSheet,$rootScope,APPCONSTANT) {
  $scope.goCoupon =function(){
    $state.go('coupon')
  };
  $scope.goCard =function(){
    $state.go('card')
  } ;

  $scope.goBillList =function(){
    $state.go('billList')
  };


  $scope.groups = [];
  for (var i=0; i<13; i++) {
    $scope.groups[i] = {
      name: i,
      items: []
    };
    for (var j=0; j<3; j++) {
      $scope.groups[i].items.push(i + '-' + j);
    }
  }

  /*
   * if given group is the selected group, deselect it
   * else, select the given group
   */
  $scope.toggleGroup = function(group) {
    if ($scope.isGroupShown(group)) {
      $scope.shownGroup = null;
    } else {
      $scope.shownGroup = group;
    }
  };
  $scope.isGroupShown = function(group) {
    return $scope.shownGroup === group;
  };

  $scope.goChooseReason =function(){
    var hideSheet = $ionicActionSheet.show({
      buttons: [
        {text: '<b>请选择</b> '},
        {text: '<b>退运费</b> '},
        {text: '<b>收到商品破损</b>'},
        {text: '<b>商品错发/漏发</b> '},
        {text: '<b>商品需要维修</b>'},
        {text: '<b>发票问题</b> '}

      ],
      cancel: function () {
        // add cancel code..
      },
      buttonClicked: function (index) {
        console.log(index);
        return true
      }
    });
  };


  //20160831
  $scope.loginOut = function () {
    LocalStorageService.cleanObject(USERAUTHDATA);
    $state.go('tab.home')
  }
  $scope.userInfo ={}
  function getUserInfo(){
    TokenService.getLocalUserId().then(function (user_id) {
      TokenService.getLocalToken().then(function (token) {
        UserService.getUserInfo(token,user_id).then(function (data) {
          $scope.userInfo = data;
          $scope.isDeal = $scope.userInfo.deal;//是否担保
          $scope.isRealInfo = $scope.userInfo.real_info;//是否实名
        })
      })
    })
  }
  getUserInfo();
  //修改用户头像操作
  var $inputImage = $('#inputImage');
  $inputImage.change(function () {
    var self = this ;
    var files = self.files;
    $rootScope.userImg = files;
    $state.go('updateuserimage');
  });


  //实名认证
  $scope.authenticationData = {}
  $scope.isDeal = false;//是否担保
  $scope.isRealInfo = false;//是否实名
  $scope.version = APPCONSTANT.version;
  $scope.clickAuthentication = function () {
    if(!$scope.authenticationData.name || !$scope.authenticationData.id) return;
    TokenService.getLocalUserId().then(function (user_id) {
      TokenService.getLocalToken().then(function (token) {
      UserService.realInfoUser(user_id,$scope.authenticationData.name,$scope.authenticationData.id,token).then(function (res) {
        if(res.response == 1){
          console.log("实名认证成功")
        }
      })
      })
    })
  }
  $scope.identification = function(){
    if($scope.isRealInfo == false)
    $state.go('identification')
  }
});
